package com.park.web.service.impl;

import com.park.web.base.baseOperation.BaseService;
import com.park.web.common.enumOrgType;
import com.park.web.dao.pbaParkingOrganizationInfoMapper;
import com.park.web.model.domain.basSystemDataDict;
import com.park.web.model.domain.pbaParkingOrganizationInfo;
import com.park.web.core.support.IService;
import com.park.web.model.vo.basSystemDataDictVo;
import com.park.web.model.vo.pbaParkingOrganizationInVo;
import com.park.web.model.vo.pbaParkingOrganizationTreeVo;
import com.park.web.service.pbaParkingOrganizationInfoService;
import org.aspectj.lang.annotation.AdviceName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class pbaParkingOrganizationInfoServiceImpl extends BaseService<pbaParkingOrganizationInfo> implements pbaParkingOrganizationInfoService {

    @Autowired
    pbaParkingOrganizationInfoMapper ipbaParkingOrganizationInfoMapper;

    public List<pbaParkingOrganizationInVo> selectAllList(String orgType) {
        var actionList = ipbaParkingOrganizationInfoMapper.selectAll();
        if (null != orgType)
            actionList = actionList.stream().filter(x -> x.getOrganizationTypeCode().equals(orgType)).collect(Collectors.toList());
        return this.getTree(actionList, "-1");
    }

    private List<pbaParkingOrganizationInVo> getTree(List<pbaParkingOrganizationInfo> list, String parentModuleKey) {
        List<pbaParkingOrganizationInfo> listChildrens = null;
        if (parentModuleKey == null) {
            listChildrens = list.stream().filter(x -> x.getParentCode() == parentModuleKey).collect(Collectors.toList());
        } else {
            list.removeIf(x -> x.getParentCode() == null);
            listChildrens = list.stream().filter(x -> x.getParentCode().equals(parentModuleKey)).collect(Collectors.toList());
        }
        List<pbaParkingOrganizationInVo> listVo = new ArrayList<pbaParkingOrganizationInVo>();
        for (var children : listChildrens) {
            pbaParkingOrganizationInVo entity = new pbaParkingOrganizationInVo();
            entity.setOrganizationCode(children.getOrganizationCode());
            boolean hasChildred = parentModuleKey == null ? true : list.stream().filter(x -> x.getParentCode().equals(children.getOrganizationCode())).count() > 0;
            entity.setHasChildren(hasChildred);
            entity.setOrganizationName(children.getOrganizationName() + "(" + children.getOrganizationCode() + ")");
            entity.setOrganizationType(children.getOrganizationTypeCode());
            var tempList = getTree(list, children.getOrganizationCode());
            if (tempList.size() > 0)
                entity.setChild(tempList);
            listVo.add(entity);
        }
        return listVo;
    }

    public List<pbaParkingOrganizationTreeVo> selectAllOrganization() {
        return ipbaParkingOrganizationInfoMapper.selectAllOrganization();
    }
}